package com.seaking.dao;

import com.seaking.pojo.Data;
import com.seaking.util.Page;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by wchha on 10/24 0024.
 */
@Repository
public class DataDao {

    @Autowired
    private HibernateTemplate hibernateTemplate;

    @Autowired
    private SessionFactory sessionFactory;

    public Long getCount() {
        Long next = (Long) hibernateTemplate.find("select count (id) from com.seaking.pojo.Data").listIterator().next();
        System.out.println(next);
        return next;
    }

    public List<Data> getAll(Page page) {
        Session session = sessionFactory.openSession();
        List list1 = session.createQuery("from com.seaking.pojo.Data order by time desc").setFirstResult((page.getPageNum()-1)*page.getPageSize()).setMaxResults(12).list();
        session.close();
        return list1;
    }

    @Transactional
    public void saveData(Data data1) {
        hibernateTemplate.save(data1);
    }

    public Data showDetail(Integer id) {
        Data data = hibernateTemplate.get(Data.class, id);
        return data;
    }

    public List<Data> getTitle() {
        Session session = sessionFactory.openSession();
        List<Data> list1 = (List<Data>) session.createQuery("select title,id from com.seaking.pojo.Data order by good desc").setMaxResults(12).list();
        session.close();
        return list1;
    }

    @Transactional
    public void updateGood(Integer id) {
        Data data = hibernateTemplate.get(Data.class, id);
        data.setGood(data.getGood() + 1);
        hibernateTemplate.update(data);
    }

    @Transactional
    public void updateClick(Data id) {
        hibernateTemplate.update(id);
    }

    public List<Data> getChose(Data data, Page page) {
        String sql = "from com.seaking.pojo.Data where 1=1";
        Session session = sessionFactory.openSession();
        if (data.getTime() != null && !data.getTime().equals("")) {
            sql = sql + " and time = " + "'" + data.getTime() + "'";
        }
        if (data.getDepartment() != null && !data.getDepartment().equals("")) {
            sql = sql + " and department = " + "'" + data.getDepartment() + "'";
        }
        if (data.getPlatform() != null && !data.getPlatform().equals("")) {
            sql = sql + " and platform = " + "'" + data.getPlatform() + "'";
        }
        if (data.getSchool() != null && !data.getSchool().equals("")) {
            sql = sql + " and school = " + "'" + data.getSchool() + "'";
        }
        List<Data> list = (List<Data>) session.createQuery(sql + " order by time desc").setFirstResult((page.getPageNum()-1)*page.getPageSize()).setMaxResults(12).list();
        session.close();
        return list;
    }
}
